<?php
/*
Plugin Name: BPDEV-SUBSCRIBE
Plugin URI: http://buddypressdev.org/projects/#subscribe
Description: Allows members to subscribe to a blog
Author: Nicola Greco
Version: 0.3
Author URI: http://nicolagreco.com

Copyright (c) 2009-2011, Nicola Greco (mail: notsecurity@gmail.com | website: http://nicolagreco.com).

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

define( 'BPDEV_SUBSCRIBE_NAME', 'BPDEV-SUBSCRIBE' );
define( 'BPDEV_SUBSCRIBE_VERSION', '0.3' );
define( 'BPDEV_SUBSCRIBE_DB_VERSION' , '0.2' );

require_once( 'bp-core.php' );
require_once( 'bpdev-core.php' );

require_once( 'bpdev-subscribe/bpdev-subscribe-widget.php' );
require_once( 'bpdev-subscribe/bpdev-subscribe-classes.php' );
require_once( 'bpdev-subscribe/bpdev-subscribe-notifications.php' );

if ( !get_site_option( 'bpdev-subscribe-status' ) ) add_option( 'bpdev-subscribe-status' , 'off' );

add_action( 'bpdev_admin_plugins_screen' , 'bpdev_subscribe_admin_menu_screen', 3 );
add_action( 'bpdev_admin_plugins_save' , 'bpdev_subscribe_admin_menu_save', 3 );

if ( 'on' == get_site_option( 'bpdev-subscribe-status' ) ) {

	add_action( 'admin_menu', 'bpdev_subscribe_check_installed' );

	add_action( 'wp', 'bpdev_subscribe_setup_globals', 1 );	
	add_action( 'admin_menu', 'bpdev_subscribe_setup_globals', 1 );

	add_action( 'bpdev_subscribe_activation' , 'bpdev_subscribe_activation_links' );
	add_action( 'bpdev_subscribe_deactivation' , 'bpdev_subscribe_deactivation_links' );

	if ( 'on' == get_site_option( 'bpdev-subscribe-buddybar-status' ) )
		add_action( 'bp_adminbar_menus' , 'bpdev_subscribe_buddybar_links', 10 );

}

function bpdev_subscribe_setup_globals() {

	global $bp;

	$bp->bpdev->subscribe->slug = 'subscribe';
	$bp->bpdev->subscribe->table_name = "wp_{$bp->bpdev->prefix->tables}{$bp->bpdev->subscribe->slug}_meta";

}

function bpdev_subscribe_install() {

	global $wpdb, $bp;
	
	if ( !empty( $wpdb->charset ) )
		$charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
		
	$sql[] = "CREATE TABLE {$bp->bpdev->subscribe->table_name} (
			id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
			blog_id bigint(20) NOT NULL,
			meta_key varchar(255) DEFAULT NULL,
			meta_value longtext DEFAULT NULL,
			KEY blog_id (blog_id),
			KEY meta_key (meta_key)
		   ) {$charset_collate};";

	require_once( ABSPATH . 'wp-admin/upgrade-functions.php' );

	dbDelta( $sql );
	
	update_site_option( 'bpdev-subscribe-db-version', BPDEV_SUBSCRIBE_DB_VERSION );

}

function bpdev_subscribe_check_installed() {

	global $wpdb, $bp;

	if ( is_site_admin() ) {

		if ( ( !$wpdb->get_var( "SHOW TABLES LIKE '{$bp->bpdev->subscribe->table_name}'" ) ) || ( get_site_option( 'bpdev-subscribe-db-version' ) < BPDEV_SUBSCRIBE_DB_VERSION )  )
			bpdev_subscribe_install();
	}

}

function bpdev_subscribe_admin_menu_save() {

	do_action( 'bpdev_admin_save_component' , 'bpdev-subscribe-status', false );
	do_action( 'bpdev_admin_save_component' , 'bpdev-subscribe-buddybar-status', false );

}

function bpdev_subscribe_admin_menu_screen() {

	?>
	<h3><?php echo BPDEV_SUBSCRIBE_NAME; ?></h3>
	<table class="form-table">

		<tr valign="top">

			<th scope="row">Plugin Version</th>

			<td>
				<?php echo BPDEV_SUBSCRIBE_VERSION; ?>
			</td>

		</tr>
		<tr>

			<th scope="row">Donators (more than 100USD):</th>

			<td>
				<a href="http://jungundwillich.de">wildrot</a>
			</td>

		</tr>
		<tr>

			<th scope="row">Plugin Status</th>

			<td>

				<select id="bpdev-subscribe-status" name="bpdev-subscribe-status">

					<option value="off"  <?php if (!get_site_option('bpdev-subscribe-status') || "off" ==  get_site_option( 'bpdev-subscribe-status' ) ) echo 'selected=""'; ?>>Off</option>

					<option value="on" <?php if ( "on" ==  get_site_option( 'bpdev-subscribe-status' ) ) echo 'selected=""'; ?>>On</option>

				</select>

			</td>

		</tr>
		<tr>

			<th scope="row">Active on BuddyBar</th>

			<td>

				<select id="bpdev-subscribe-buddybar-status" name="bpdev-subscribe-buddybar-status">

					<option value="off"  <?php if ( !get_site_option( 'bpdev-subscribe-buddybar-status' ) || 'off' == get_site_option( 'bpdev-subscribe-buddybar-status' ) ) echo 'selected=""'; ?>>Off</option>

					<option value="on" <?php if ( 'on' == get_site_option( 'bpdev-subscribe-buddybar-status' ) ) echo 'selected=""'; ?>>On</option>

				</select>

			</td>

		</tr>

	</table>
	<?php

}

function bpdev_subscribe_activation_links() {

	?>

		<a href="?bpdev_subscribe=on">Switch on subscription</a>

	<?php

}

function bpdev_subscribe_deactivation_links() {

	?>

		<a href="?bpdev_subscribe=off">Switch off subscription</a>

	<?php

}

function bpdev_subscribe_process_subscription( $user = false, $blog = false ) {

	global $bp, $current_blog;

	if ( !$user && !$blog ) {
		$user = $bp->loggedin_user->id;
		$blog = $current_blog->blog_id;
	}

	if ( 'on' == $_GET['bpdev_subscribe'] ) {

		if ( !BPDEV_Subscribe::check( $user, $blog ) )
			BPDEV_Subscribe::subscribe( $user, $blog );

	} elseif ( 'off' == $_GET['bpdev_subscribe'] ) {

		if ( BPDEV_Subscribe::check( $user, $blog ) )
			BPDEV_Subscribe::unsubscribe( $user, $blog );

	}


}

function bpdev_subscribe_buddybar_links() {

	global $bp, $current_blog;

	 bpdev_subscribe_process_subscription();

	?>

	<li id="bpdev-admin-subscribe_links"><a href="/">Subscription</a>
		<ul>
			<li>

	<?php

	if ( BPDEV_Subscribe::check( $bp->loggedin_user->id, $current_blog->blog_id ) )
		do_action('bpdev_subscribe_deactivation');
	else
		do_action('bpdev_subscribe_activation');

	?>

			</li>
		</ul>
	</li>

	<?php

}

function bpdev_subscribe_addmeta( $user_id, $blog_id ) {

	global $bp;

	return bpdev_framework_addmeta( $blog_id , 'hasSubscriber' , $user_id , 'blog_id' , $bp->bpdev->subscribe->table_name );

}

function bpdev_subscribe_deletemeta( $user_id, $blog_id ) {

	global $bp;

	return bpdev_framework_deletemeta( $blog_id , 'hasSubscriber' , $user_id , 'blog_id' , $bp->bpdev->subscribe->table_name );

}

function bpdev_subscribe_getmeta( $stuff_id, $meta_value = false, $type = true ) {

	global $bp;

	if ( $type )
		return bpdev_framework_getmeta( $stuff_id , 'hasSubscriber' , $meta_value , 'blog_id' , $bp->bpdev->subscribe->table_name );
	else
		return bpdev_framework_getmeta( false , 'hasSubscriber' , $user_id , 'meta_value' , $bp->bpdev->subscribe->table_name , 'blog_id' );

}
?>